home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / cpu / mips / dismips.c next >
C/C++ Source or Header  |  2000-05-10  |  3KB  |  133 lines

  1. /*
  2.  * standalone MIPS disassembler by smf
  3.  *
  4.  * based on DIS68k by Aaron Giles
  5.  *
  6.  */
  7.  
  8.  
  9. #include "osd_cpu.h"
  10. static UINT8 *filebuf;
  11. static UINT32 offset;
  12. static UINT8 order[] = { 3, 2, 1, 0 };
  13.  
  14. #define STANDALONE
  15. #include "mipsdasm.c"
  16.  
  17. static char *Options[]=
  18. {
  19.     "begin","end","offset","order",0
  20. };
  21.  
  22. static void usage (void)
  23. {
  24.     printf ("Usage: DISMIPS [options] <filename>\n\n"
  25.             "Available options are:\n"
  26.             " -begin  - Specify begin offset in file to disassemble in bytes [0]\n"
  27.             " -end    - Specify end offset in file to disassemble in bytes [none]\n"
  28.             " -offset - Specify address to load program in bytes [0]\n"
  29.             " -order  - Specify byte order [3210]\n\n"
  30.             "All values should be entered in hexadecimal\n");
  31.     exit (1);
  32. }
  33.  
  34. int main (int argc,char *argv[])
  35. {
  36.     UINT8 i,j,n,p;
  37.     char *filename=0,buf[80];
  38.     FILE *f;
  39.     UINT32 begin=0,end=(UINT32)-1,filelen,len,pc;
  40.  
  41.     n=0;
  42.     for (i=1;i<argc;++i)
  43.     {
  44.         if (argv[i][0]!='-')
  45.         {
  46.             switch (++n)
  47.             {
  48.             case 1:  filename=argv[i]; break;
  49.             default: usage();
  50.             }
  51.         }
  52.         else
  53.         {
  54.             for (j=0;Options[j];++j)
  55.                 if (!strcmp(argv[i]+1,Options[j])) break;
  56.  
  57.             switch (j)
  58.             {
  59.             case 0: ++i; if (i>argc) usage();
  60.                 begin=strtoul(argv[i],0,16);
  61.                 break;
  62.             case 1:  ++i; if (i>argc) usage();
  63.                 end=strtoul(argv[i],0,16);
  64.                 break;
  65.             case 2:  ++i; if (i>argc) usage();
  66.                 offset=strtoul(argv[i],0,16);
  67.                 break;
  68.             case 3: ++i; if (i>argc) usage();
  69.                 if( strlen( argv[ i ] ) != 4 )
  70.                 {
  71.                     usage();
  72.                 }
  73.                 for( p=0; p < 3; p++ )
  74.                 {
  75.                     if( argv[ i ][ p ] < '0' || argv[ i ][ p ] > '3' )
  76.                     {
  77.                         usage();
  78.                     }
  79.                     order[ p ] = argv[ i ][ p ] - '0';
  80.                 }
  81.                 break;
  82.             default: usage();
  83.             }
  84.         }
  85.     }
  86.  
  87.     if (!filename)
  88.     {
  89.         usage();
  90.         return 1;
  91.     }
  92.     f=fopen (filename,"rb");
  93.     if (!f)
  94.     {
  95.         printf ("Unable to open %s\n",filename);
  96.         return 2;
  97.     }
  98.     fseek (f,0,SEEK_END);
  99.     filelen=ftell (f);
  100.     fseek (f,begin,SEEK_SET);
  101.     len=(filelen>end)? (end-begin+1):(filelen-begin);
  102.     filebuf=malloc(len+16);
  103.     if (!filebuf)
  104.     {
  105.         printf ("Memory allocation error\n");
  106.         fclose (f);
  107.         return 3;
  108.     }
  109.     memset (filebuf,0,len+16);
  110.     if (fread(filebuf,1,len,f)!=len)
  111.     {
  112.         printf ("Read error\n");
  113.         fclose (f);
  114.         free (filebuf);
  115.         return 4;
  116.     }
  117.     fclose (f);
  118.     pc=0;
  119.     while (pc<len-1)
  120.     {
  121.         i=(DasmMIPS (buf,pc+offset));
  122.  
  123.         printf ("%08x: ",pc+offset);
  124.         for (j=0;j<i ;++j) printf("%02x ",filebuf[pc+(j/4)*4+order[j%4]]);
  125.         for (   ;j<10;++j) printf("   ");
  126.         printf(buf);
  127.         printf ("\n");
  128.         pc+=i;
  129.     }
  130.     free (filebuf);
  131.     return 0;
  132. }
  133.